Fix issue #2353: Incorrect canonicalization of NATURAL JOIN with INNE…#2355
Fix issue #2353: Incorrect canonicalization of NATURAL JOIN with INNE…#2355revitalkr wants to merge 1 commit into
Conversation
…h INNER JOIN in Snowflake dialect produces non-equivalent query
| // canonical string without parentheses | ||
| "SELECT * FROM t1 NATURAL JOIN t5 INNER JOIN t0 ON (t0.v1 + t5.v0) > 0 WHERE t0.v1 = t1.v0", | ||
| ); | ||
| all_dialects_except(|d| d.supports_left_associative_joins_without_parens()).verified_query_with_canonical( |
There was a problem hiding this comment.
we seem to be removing test coverage for the feature supports_left_associative_joins_without_parens - I think we might want to replace the removal with something else? otherwise its not clear to me if/why we need to keep the dialect flag around
There was a problem hiding this comment.
It is used for queries with join which is not natural join, there's a snowflake dialect test for it, also I added a test for it in my next PR in the common file which is using this feature
There was a problem hiding this comment.
I guess I'm expecting us to have a test somewhere in the codebase that uses all_dialects_except(|d| d.supports_left_associative_joins_without_parens())? Otherwise it looks like we're not testing that feature?
…R JOIN in Snowflake dialect produces non-equivalent query
Closes #2353
The current canonicalization rewrites queries such as:
SELECT *
FROM t1
NATURAL JOIN t5
INNER JOIN t0 ON (t0.v1 + t5.v0) > 0
into:
t1 NATURAL JOIN (t5 INNER JOIN t0 ...)
This transformation is not semantics-preserving.
NATURAL JOIN implicitly generates join predicates based on all shared column names.
Moving t0 into the right-hand side of the NATURAL JOIN changes the set of visible columns, which may introduce additional implicit join conditions and lead to different results.
Minimal example (see issue for full repro):
SELECT *
FROM t1
NATURAL JOIN t5
INNER JOIN t0 ON (t0.v1 + t5.v0) > 0
Expected behavior:
The canonicalization should preserve semantics, e.g.:
(t1 NATURAL JOIN t5) INNER JOIN t0 ...
Fix:
Avoid right-associative parsing when NATURAL JOIN is present, ensuring that NATURAL JOIN is evaluated left-to-right.
Tests:
join_precedencetest, which previously assumed incorrect Snowflake behavior.